﻿using QuotingTool.DataModel;
using QuotingTool.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuotingTool.Domain.Models
{
    public class MaintenanceDetailsData
    {
        public static DataTable GetMaintenanceDetailsData(DBQuote Quote)
        {
            List<DBSubLineItem> SubLines = Quote.SubLines;
            CultureInfo ci = new CultureInfo("en-US");
            List<MaintenanceDetails> maintenanceDetailsData = new List<MaintenanceDetails>();

            foreach (DBSubLineItem item in SubLines)
            {

                MaintenanceDetails data = new MaintenanceDetails();
                
                if(!string.IsNullOrEmpty(item.START_DATE))
                {
                    data.StartDate = DateTime.Parse(item.START_DATE).ToString("dd MMM yyyy", ci);
                }

                if (!string.IsNullOrEmpty(item.END_DATE))
                {
                    data.EndDate = DateTime.Parse(item.END_DATE).ToString("dd MMM yyyy", ci);
                }

                data.Currency = Quote.Header.CURRENCY_CODE;
                data.ProductName = item.COVERED_PRODUCT;
                data.NumberOFLicenses = item.NUMBER_LICENCES;
                data.Quantity = item.QUANTITY;
                data.InstalledAtCountry = item.INSTALLED_AT_COUNTRY;
                data.InstalledAtCity = item.INSTALLED_AT_CITY;
                data.ShipToCustomer = item.SHIP_TO_CUSTOMER_NAME;
                data.BillToCustomer = item.BILL_TO_CUSTOMER_NAME;
                data.ProductInitialPO = item.PRODUCT_INITIAL_PO_NUMBER;
                data.SourceContract = item.SOURCE_CONTRACT_NUMBER;
                data.SolutionVersion = item.SOLUTION_VERSION;


                if (!string.IsNullOrEmpty(item.PRODUCT_ORIG_SHIP_DATE))
                {
                    data.OrignalShippingDateOfProduct = DateTime.Parse(item.PRODUCT_ORIG_SHIP_DATE).ToString("dd MMM yyyy", ci);
                }
                
                data.SerialNumber = item.SERIAL_NUMBER;
                data.InstanceNumber = item.INSTANCE_NUMBER;
             
                data.Description= "";
                data.RenewalPeriodInDays = item.DURATION;
                data.PolicyRenewalRatMARenewalRatePCT = item.MAINTENANCE_RATE;
                data.MaintenancePricebeforeCOL = item.CURRENT_AMOUNT;
                data.SourceSalesOrder = item.SALES_ORDER_NUMBER;
                data.PolicyCOLORMACOL = item.COL;
                data.AnnualMaintenanceCostCOL = item.ANNUAL_MAINT_PRICE_INCL_COL;
                data.SourceContractPO = item.SOURCE_CONTRACT_PO_NUM;
                data.MaintenanceSource = item.MAINTENANCE_SOURCE;
                if (!string.IsNullOrEmpty(item.MAINT_PRICE_INCLUDING_COL))
                {
                    data.ProratedMaintenanceCost = double.Parse(item.MAINT_PRICE_INCLUDING_COL);
                }

                maintenanceDetailsData.Add(data);
            }
           
            return Common.ToDataTable(maintenanceDetailsData);
        }
    }
}
